package com.fanco.learn_datastruct_leetcode.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author qiaowang 2021/5/12
 * 给你一个包含 n 个整数的数组nums，判断nums中是否存在三个元素 a，b，c ，使得a + b + c = 0 ？请你找出所有和为 0 且不重复的三元组。
 * 注意：答案中不可以包含重复的三元组。
 */
public class Day09 {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {-1, 3, -2, 0, 5, -6, 1};
        System.out.println(solution.threeSum(nums));
    }

    private static class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            int n = nums.length;
            List<List<Integer>> result = new ArrayList<>();
            if (n <= 3 || nums == null) {
                return result;
            }
            Arrays.sort(nums);
            for (int i = 0; i < n; i++) {
                if (nums[i] > 0) {
                    return result;
                }
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                int target = -nums[i];
                int left = i + 1;
                int right = nums.length - 1;
                while (left < right) {
                    if (nums[left] + nums[right] == target) {
                        result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                        left++;
                        right--;
                        while (left < right && nums[left] == nums[left - 1]) {
                            left++;
                        }
                        while (left < right && nums[right] == nums[right + 1]) {
                            right--;
                        }
                    } else if (nums[left] + nums[right] < target) {
                        left++;
                    } else if (nums[left] + nums[right] > target) {
                        right--;
                    } else {
                    }
                }
            }
            return result;
        }

        public List<List<Integer>> threeSum2(int[] nums) {
            List<List<Integer>> list = new ArrayList<>();
            int n = nums.length;
            if (nums == null || n <= 3) {
                return list;
            }

            Arrays.sort(nums);
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] > 0) {
                    break;
                }
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }

                int left = i + 1;
                int right = nums.length - 1;
                int target = nums[i];

                while (left < right) {
                    if (nums[left] + nums[right] == target) {
                        list.add(Arrays.asList(nums[i], nums[left], nums[right]));
                        left++;
                        right--;

                        while(left < right && nums[left] == nums[left-1]) {
                            left++;
                        }
                        while(left < right && nums[right] == nums[right-1]) {
                            right--;
                        }
                    } else if (nums[left] + nums[right] < target) {
                        left++;

                    } else if (nums[left] + nums[right] > target) {
                        right--;
                    }
                }
            }

            return list;
        }
    }
}
